//
//  NewFeaturesController.swift
//  WeiBo
//
//  Created by 江潘 on 15/6/28.
//  Copyright © 2015年 江潘. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeaturesController: UICollectionViewController {
    
    var imgCount = 4
    
    override  init(collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = layout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = UIScreen.mainScreen().bounds.size
        
        flowLayout.minimumInteritemSpacing = 0
        
        flowLayout.minimumLineSpacing = 0
        
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        super.init(collectionViewLayout: flowLayout)
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView?.bounces = false
        
        collectionView?.showsHorizontalScrollIndicator = false
        
        collectionView?.pagingEnabled = true
        
        collectionView?.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imgCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
        
        cell.imgIndex = indexPath.item
        
        return cell
    }
   
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return 1
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last!
        
        if path.item == imgCount - 1 {
        
            (collectionView.cellForItemAtIndexPath(path) as! CollectionViewCell).showStartBtn()
        }
    }
    
    
    
}
